package com.slz3.business.module.afterSaleManagement.aftWorkOrder.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.afterSaleManagement.aftWorkOrder.domain.AftWorkOrder;
import com.slz3.business.module.afterSaleManagement.aftWorkOrder.domain.AftWorkOrderFactory;
import com.slz3.business.module.afterSaleManagement.aftWorkOrder.steward.AftWorkOrderSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
public class AftWorkOrderServiceImpl implements AftWorkOrderService {
    @Override
    public AftWorkOrder save(AftWorkOrder aftWorkOrder) {
        return Repositorys.aftWorkOrder.save(AftWorkOrderFactory.initBaseFields(aftWorkOrder));
    }

    @Override
    public Page<AftWorkOrder> page(MultiValueMap<String, String> params) {
        Page<AftWorkOrder> aftWorkOrderPage = Repositorys.aftWorkOrder.findAll(
                AftWorkOrderSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return aftWorkOrderPage;
    }


    @Override
    public AftWorkOrder modify(AftWorkOrder aftWorkOrderNew) {
        return Repositorys.aftWorkOrder.saveAndFlush(AftWorkOrderFactory.initBaseFields(aftWorkOrderNew));
    }

    @Override
    public Optional<AftWorkOrder> single(UUID uuid) {
        return Repositorys.aftWorkOrder.findById(uuid);
    }

    @Override
    public List<AftWorkOrder> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.aftWorkOrder.findAllById(uuids);
    }

    @Override
    public List<AftWorkOrder> delete(List<AftWorkOrder> aftWorkOrderList) {
        aftWorkOrderList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            AftWorkOrderFactory.initBaseFields(item);
        });
        return Services.aftWorkOrderTransactional.batchModify(aftWorkOrderList);
    }

}
